package com.chanyachting.emall.serviceImpl;

import java.util.List;

import org.hibernate.Query;

import com.chanyachting.emall.dao.ShowyachtDAO;
import com.chanyachting.emall.domain.Showyacht;
import com.chanyachting.emall.service.ShowyachtService;

public class ShowyachtServiceImpl implements ShowyachtService {

	private ShowyachtDAO cDao;

	public ShowyachtServiceImpl() {

		this.cDao = new ShowyachtDAO();
	}
	
	@SuppressWarnings("unchecked")
	public List<Showyacht> getShowyachtListByPage(int currentPage,int pageSize,String channelId,boolean recommendFlg) {
		StringBuffer sb = new StringBuffer();
		
		sb.append("from Showyacht where");
		if (recommendFlg) {
			sb.append(" recommend = '1'");
		} else {
			sb.append(" status = '01'");
		}
		if (channelId != null && !"".equals(channelId )) {
			sb.append(" and channelId = " + channelId);
		}
		sb.append(" order by updateDate desc");
		Query q = this.cDao.createQuery(sb.toString());
		
		q.setFirstResult((currentPage - 1) * pageSize);
		q.setMaxResults(pageSize);
		
		return q.list();
	}

	public int getShowyachtCount(String channelId,boolean recommendFlg) {
		String sql = "select count(*) from Showyacht where";
		if (recommendFlg) {
			sql = sql + " recommend = '1'";
		} else {
			sql = sql + " status = '01'";
		}
		if (channelId != null && !"".equals(channelId )) {
			sql = sql + " and channelId = " + channelId;
		}
		
		return (Integer)cDao.find(sql).get(0);
	}
	
	@SuppressWarnings("unchecked")
	public Showyacht getOneShowyachtById(String id) {
		String sql = "from Showyacht where id = ? and status = '01'";
		
		List<Showyacht> showyachtList = cDao.find(sql, new String[]{id});
		
		if (showyachtList == null || showyachtList.size() != 1) {
			return null;
		} else {
			return showyachtList.get(0);
		}
	}
}
